#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_Image.h>
#include <windows.h>
#include "constantes.h"
#include "fichiers.h"
#include "jeu.h"

extern SDL_Surface* screen;

void loadGame()
{
    int map[NB_BLOCS];
    int i;
    SDL_Surface* tiles[5];
    SDL_Surface* marioTiles[4];
    SDL_Rect marioPos;

    loadTiles(tiles, marioTiles);
    if (!getMap(map))
    {
        error("can't open levels.msf\n");
    }
    for (i = 0 ; i < NB_BLOCS ; i++)
    {
        if (map[i] == MARIO)
        {
            marioPos.x = (i % NB_BLOCS_LARGEUR) * TAILLE_BLOC;
            marioPos.y = i / NB_BLOCS_LARGEUR * TAILLE_BLOC;
            map[i] = VIDE;
        }
    }
    play(map, tiles, marioTiles, marioPos);
}

void play(int map[], SDL_Surface* tiles[], SDL_Surface* marioTiles[], SDL_Rect marioPos)
{
    int marioDirection = BAS;
    int prev = (int)GetTickCount(), actu = (int)GetTickCount();
    SDL_Event mov;

    while (!haveWon(map) && SDL_WaitEvent(&mov))
    {
        actu = (int)GetTickCount();
        printf("%d - %d = %d\n", prev, actu, actu-prev);
        prev = (int)GetTickCount();
        displayMap(map, tiles);
        SDL_BlitSurface(marioTiles[marioDirection], NULL, screen, &marioPos);
        SDL_Flip(screen);
        switch (mov.type)
        {
            case SDL_QUIT:
                quit();
            case SDL_KEYDOWN:
                switch (mov.key.keysym.sym)
                {
                    case SDLK_UP:
                        marioDirection = HAUT;
                        if (marioPos.y == 0)
                            break;
                        if (map[getMarioPos(marioPos) - NB_BLOCS_LARGEUR] == VIDE)
                            marioPos.y -= TAILLE_BLOC;
                        break;
                    case SDLK_DOWN:
                        marioDirection = BAS;
                        if (marioPos.y == TAILLE_BLOC * (NB_BLOCS_HAUTEUR - 1))
                            break;
                        if (map[getMarioPos(marioPos) + NB_BLOCS_LARGEUR] == VIDE)
                            marioPos.y += TAILLE_BLOC;
                        break;
                    case SDLK_LEFT:
                        marioDirection = GAUCHE;
                        if (marioPos.x == 0)
                            break;
                        if (map[getMarioPos(marioPos) - 1] == VIDE)
                            marioPos.x -= TAILLE_BLOC;
                        break;
                    case SDLK_RIGHT:
                        marioDirection = DROITE;
                        if (marioPos.x == TAILLE_BLOC * (NB_BLOCS_LARGEUR -1))
                            break;
                        if (map[getMarioPos(marioPos) + 1] == VIDE)
                            marioPos.x += TAILLE_BLOC;
                        break;
                    case SDLK_ESCAPE:
                        return;
                    default: ;
                }
        }
    }
}

int haveWon(int map[])
{
    int i;

    for (i = 0 ; i < NB_BLOCS ; i++)
        if (map[i] == OBJECTIF)
            return 0;

    return 1;
}

int getMarioPos(SDL_Rect marioRectPos)
{
    return (marioRectPos.y * NB_BLOCS_LARGEUR + marioRectPos.x) / TAILLE_BLOC;
}
